$:.unshift File.join(File.dirname(__FILE__), "..", "lib/rwebunit")
$:.unshift File.dirname(__FILE__)

require "test_utils.rb"
require 'test/unit'

class TestWebTestCase < Test::Unit::TestCase
  include RWebUnit::Utils

  def setup
  end

  def teardown
  end

  def test_getToday
    today_str = sprintf("%02d/%02d/%04d", Time.now.day, Time.now.month, Time.now.year)
    assert_equal(today_str, getToday)
  end

  def test_getDaysBefore
    yesterday = Time.now - 24 * 3600 * 1
    yesterday_str = sprintf("%02d/%02d/%04d", yesterday.day, yesterday.month, yesterday.year)
    assert_equal(yesterday_str, getDaysBefore(1))
    assert_equal(yesterday_str, getYesterday)
  end
  
  def test_getDaysAfter
    tomorrow = Time.now + 24 * 3600 * 1
    tomorrow_str = sprintf("%02d/%02d/%04d", tomorrow.day, tomorrow.month, tomorrow.year)
    assert_equal(tomorrow_str, getDaysAfter(1))
    assert_equal(tomorrow_str, getTomorrow)
  end
  
  def test_randomBoolean
    true_count = 0
    false_count = 0
    100.times {
      obj = randomBoolean
      assert(obj.instance_of?(TrueClass) || obj.instance_of?(FalseClass), "can only be true or false")
      true_count += 1 if obj
      false_count += 1 if !obj
    }
    assert(true_count > 0, "it is not random")
    assert(false_count > 0, "it is not random")
  end

  def test_randomNumber
    tmp_array = []
    1000.times {
      num = randomNumber(1,10)
      assert(num.instance_of?(Fixnum), "can only be number")
      tmp_array << num
    }
    uniq_numbers = tmp_array.uniq.sort
    assert_equal(10, uniq_numbers.length)
    assert_equal(1, uniq_numbers[0])
    assert_equal(10, uniq_numbers[9])
  end

  def test_randomDigit
    tmp_array = []
    1000.times {
       dc = randomDigit()
       tmp_array << dc
    }
    uniq_digits = tmp_array.uniq.sort
    assert_equal(10, uniq_digits.length)
    assert_equal('0', uniq_digits[0])
    assert_equal('9', uniq_digits[9])
  end

end
